#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
const int num = 5;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* Wait(void* name){
    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        std::cout << "I am: " << static_cast<char*>(name) << std::endl;
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}
int main()
{
    pthread_t pthreads[num];
    for(int i = 0; i < num; i++)
    {
        char* name = new char[1024];
        snprintf(name, 1024, "Thread_%d", i + 1);
        pthread_create(&pthreads[i], nullptr, Wait, name);
        usleep(10000);
    }
    while(true)
    {
        std::cout << "唤醒一个: " << std::endl;
        pthread_cond_broadcast(&cond);
        sleep(1);
    }
    for(int i = 0; i < num; i++)
    {
        pthread_join(pthreads[i], nullptr);
    }
}